import 'package:flutter/material.dart';
import 'package:interview/theme/app_colors.dart';

class PageMenuOverlay {
  static OverlayEntry? _overlayEntry;
  static Offset _tapPosition = Offset.zero;
  static late AnimationController _animationController;
  static late Animation<double> _opacityAnimation;

  // 显示 Overlay
  static void show({
    required AppColors theme,
    required BuildContext context,
    required Widget child,
  }) {
    if (_overlayEntry != null) return;

    final screenSize = MediaQuery.of(context).size;

    // 初始化动画控制器
    _animationController = AnimationController(
      vsync: Overlay.of(context),
      duration: const Duration(milliseconds: 180), // 动画时长
    );

    _opacityAnimation = CurvedAnimation(
      parent: _animationController,
      curve: Curves.easeInOut,
    );

    _overlayEntry = OverlayEntry(
      builder: (context) {
        double top = _tapPosition.dy + 10;
        double left = _tapPosition.dx + 10;
        
        // 处理边界情况
        if (top + 150 > screenSize.height) {
          top = screenSize.height - 150; // 保证不会超出底部
        } else if (top <= 10) {
          top = 50; // 确保顶部不会超出屏幕上方
        }

        if (left + 200 > screenSize.width) {
          left = screenSize.width - 100; // 保证不会超出右侧
        }else if (left <= 10) {
          left = 40; // 确保顶部不会超出屏幕上方
        }

        return Stack(
          children: [
            // 半透明的背景层，用于捕获点击事件
            GestureDetector(
              onTap: () {
                hide(); // 点击背景时隐藏弹框
              },
              child: Container(
                width: screenSize.width,
                height: screenSize.height,
                color: Colors.transparent, // 设置为透明的背景
              ),
            ),
            // 弹框内容
            Positioned(
              top: top,
              left: left,
              child: FadeTransition(
                opacity: _opacityAnimation, // 控制透明度动画
                child: Material(
                  color: Colors.transparent,
                  child: Container(
                    decoration: BoxDecoration(
                      color: theme.subjectPure,
                      borderRadius: BorderRadius.circular(12),
                      boxShadow: [
                        BoxShadow(
                          color: theme.contrastMix1!.withOpacity(0.4),
                          blurRadius: 4,
                          offset: Offset(2, 2),
                          blurStyle:  BlurStyle.outer
                        ),
                      ],
                    ),
                    child: child, // 动态内容由外部传入
                  ),
                ),
              ),
            ),
          ],
        );
      },
    );

    // 插入 Overlay
    Overlay.of(context).insert(_overlayEntry!);

    // 开始播放淡入动画
    _animationController.forward();
  }

  // 隐藏 Overlay
  static void hide() async {
    if (_overlayEntry != null) {
      // 播放淡出动画
      await _animationController.reverse();
      _overlayEntry?.remove();
      _overlayEntry = null;
      _animationController.dispose();

      // 重置点击位置
      _tapPosition = Offset.zero;
    }
  }

  // 捕获点击位置
  static void setTapPosition(TapDownDetails details) {
    _tapPosition = details.globalPosition;
  }
}
